import createAction from "./createAction";
import { dispatch } from "./asyncMiddleware";

export const actions = {};

/**
 * 为model内注册的reducers和flows注册相应的action
 *
 * @param {object[]} models
 */
export default function handleActions(models) {
  for (let key in models) {
    const model = models[key];
    const { namespace, reducers, flows } = model;
    let tempObj = {};

    for (let key in reducers) {
      tempObj[key] = createAction(dispatch, { namespace, type: key });
    }

    for (let key in flows) {
      tempObj[key] = createAction(dispatch, {
        namespace,
        type: key,
        isFlow: true
      });
    }

    actions[namespace] = tempObj;
  }
}
